import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { ApiService } from './../../../core/api/api.service';
import { MessageService } from './../../../core/service/message.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { I18nService } from 'src/app/core/i18n/i18n.service';
@Component({
	selector: 'app-user-mgmt',
	templateUrl: './user-mgmt.component.html',
})
export class UserMgmtComponent implements OnInit {
	userList = []; // 列表数据
	page: any = { pageNo: 1, pageSize: 10 };
	isVisible = false; // 是否显示弹出框
	modalTitle; // 弹出框标题
	isOkLoading = false; // 弹出框按钮加载动画
	tableLoading = false;
	validateForm;
	modal;
	roleList = [];
	passwordVisible = false;
	activeData: any = {};
	user;
	language:any = {};
	ngOnInit() {
		this.user = JSON.parse(sessionStorage.getItem('user'));
		this.getUserList();
		this.apiService.post('角色列表', { pageNo: 1, pageSize: 1000 }).then((res: any) => {
			this.roleList = res.data.list;
		});
		this.validateForm = this.fb.group({
			contactName: [null, [Validators.required]],
			contactTel: [null, [Validators.required]],
			loginId: [null, [Validators.required]],
			loginPw: [null, [Validators.required]],
			roleId: [null, [Validators.required]],
			status: [null, [Validators.required]],
		});
	}
	handleOk() {
		const url = this.activeData.id ? '编辑管理员' : '创建管理员';
		for (const v in this.validateForm.controls) {
			this.validateForm.controls[v].markAsDirty();
			this.validateForm.controls[v].updateValueAndValidity();
		}
		if (this.validateForm.valid) {
			this.apiService.post(url, { id: this.activeData.id, ...this.validateForm.getRawValue() }).then((res: any) => {
				this.isVisible = false;
				const text = this.activeData.id ? this.language.success2 : this.language.success1;
				this.nzMessageService.success(text);
				this.getUserList();
			});
		}
	}
	handleCancel() {
		this.isVisible = false;
	}
	addUser() {
		this.isVisible = true;
		this.modalTitle = this.language.add;
		this.validateForm.controls.loginPw.setValidators([Validators.required]);
		this.validateForm.controls.loginId.enable();
		this.validateForm.reset();
	}
	updateUser(data) {
		this.isVisible = true;
		this.modalTitle = this.language.edit;
		this.validateForm.controls.loginPw.setValidators([]);
		this.validateForm.controls.loginId.disable();
		this.validateForm.reset();
		this.activeData = { ...data };
		this.activeData.roleId = Number(this.activeData.roleId);
		console.log(this.activeData);
		this.validateForm.patchValue(this.activeData);
	}
	deleteUser(data) {
		this.activeData = data;
		this.modalService.confirm({
			nzTitle: this.modal.title,
			nzContent: this.language.modalText,
			nzOnOk: () => {
				this.apiService.post('删除管理员', { loginId: data.loginId }).then(res => {
					this.nzMessageService.success(this.language.success3);
					this.getUserList();
				});
			},
		});
	}
	getUserList(params = {}) {
		this.tableLoading = true;
		this.apiService
			.post('管理员列表', { ...params, ...this.page })
			.then((res: any) => {
				this.userList = res.data.list.filter(item => {
					return item.loginId !== 'master' || this.user.loginId === 'master';
				});
				this.tableLoading = false;
			})
			.catch(res => {
				this.tableLoading = false;
			});
	}
	constructor(private i18n:I18nService ,private router: Router, private fb: FormBuilder, private apiService: ApiService, private message: MessageService, private nzMessageService: NzMessageService, private modalService: NzModalService) {
		this.i18n.get(['system.user','modal','pagetitle']).subscribe(res=>{
			this.language = res['system.user'];
			this.modal = res.modal;
			const pagetitle = res.pagetitle;
            this.message.setBreadcrumb({
                breadcrumbs: [{ name: pagetitle.title8}],
            });
		})
	}
}
